﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DartsDetector
{
	public partial class frmDevCalc : Form
	{
		private bool listenCamTop = true;
		private bool listenCamRight = true;
		private int xCamTop;
		private int xCamRight;
		private Image oldImage;

		public frmDevCalc()
		{
			InitializeComponent();
			oldImage = pbBoard.Image;

			DrawLinePicturebox(new MouseEventArgs(MouseButtons.Left, 0, 160, 0, 0));
			listenCamTop = false;
			listenCamRight = false;
		}


		private void DrawLinePicturebox(MouseEventArgs e)
		{
			if (listenCamTop)
			{
				Bitmap b = new Bitmap(320, 240);
				Graphics g = Graphics.FromImage(b);
				g.DrawLine(new Pen(Brushes.Black), e.X, 0, e.X, 240);
				xCamTop = e.X * 2;
				pbCamTop.Image = b;
			}

			if (listenCamRight)
			{
				Bitmap b = new Bitmap(320, 240);
				Graphics g = Graphics.FromImage(b);
				g.DrawLine(new Pen(Brushes.Black), e.X, 0, e.X, 240);
				xCamRight = e.X * 2;
				pbCamRight.Image = b;
			}			

			if(listenCamRight || listenCamTop)
				CalculateDart();
		}

		private Point pCamTop = new Point(120, 10);

		private void CalculateDart()
		{
			Bitmap b = new Bitmap(oldImage);
			Graphics g = Graphics.FromImage(b);
			
			DrawCameras(g);


			//Ranges from 0 to 640
			//0 = Left of dartboard
			//320 = Bullseye
			//640 = Right of dartboard

			int topCamLineX = (int)(((float)xCamTop / (float)320) * DartboardCenterX);
			int rightCamLineY = (int)(((float)xCamRight / (float)320) * DartboardCenterY);

			Point TopCamPointEnd = new Point(topCamLineX, pbBoard.Height);
			Point RightCamPointEnd = new Point(0, rightCamLineY);

			//CamTop
			g.DrawLine(new Pen(Brushes.Purple), TopCamPoint, TopCamPointEnd);

			//CamRight
			g.DrawLine(new Pen(Brushes.Purple), RightCamPoint, RightCamPointEnd);
			
			Point pIntersect;
			if (DartsDetector.Classes.Utils.LineIntersect(out pIntersect, TopCamPoint, TopCamPointEnd, RightCamPoint, RightCamPointEnd))//Intersect
				g.FillEllipse(Brushes.Aqua, pIntersect.X - 2, pIntersect.Y - 2, 4, 4);
			
			
			pbBoard.Image = b;
		}

		private void DrawCameras(Graphics g)
		{
			//CamTop
			g.FillEllipse(Brushes.Red, TopCamPoint.X - 3, TopCamPoint.Y - 3, 6, 6);

			//CamRight
			g.FillEllipse(Brushes.Red, RightCamPoint.X - 3, RightCamPoint.Y - 3, 6, 6);
		}

		#region Form control events
		private void pbCamTop_MouseDown(object sender, MouseEventArgs e)
		{
			listenCamTop = true;
			DrawLinePicturebox(e);
		}

		private void pbCamTop_MouseMove(object sender, MouseEventArgs e)
		{
			DrawLinePicturebox(e);
		}

		private void pbCamTop_MouseUp(object sender, MouseEventArgs e)
		{
			listenCamTop = false;
		}

		private void pbCamRight_MouseDown(object sender, MouseEventArgs e)
		{
			listenCamRight = true;
			DrawLinePicturebox(e);
		}

		private void pbCamRight_MouseMove(object sender, MouseEventArgs e)
		{
			DrawLinePicturebox(e);
		}

		private void pbCamRight_MouseUp(object sender, MouseEventArgs e)
		{
			listenCamRight = false;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			CalculateDart();
		}
		#endregion

		private int DartboardCenterX
		{
			get { return Convert.ToInt32(tbDartboardCenterX.Text); }
		}
		private int DartboardCenterY
		{
			get { return Convert.ToInt32(tbDartboardCenterY.Text); }
		}
		private int CamDistance
		{
			get { return Convert.ToInt32(tbCamDistance.Text) * 10; }
		}

		private Point TopCamPoint
		{
			get { return new Point(DartboardCenterX, DartboardCenterY - (int)(ScaleFactor * CamDistance)); }
		}
		private Point RightCamPoint
		{
			get { return new Point(DartboardCenterX + (int)(ScaleFactor * CamDistance), DartboardCenterY); }
		}

		private float ScaleFactor
		{
			get
			{
				//Dartboard width (in pixels) / real life diameter in mm
				return (float)240 / (float)DiameterDartboardInMM;
			}
		}

		private int DiameterDartboardInMM = 451;
		private void button2_Click(object sender, EventArgs e)
		{
			Bitmap b = new Bitmap(oldImage);
			Graphics g = Graphics.FromImage(b);

			//Bullseye
			g.FillEllipse(Brushes.Aqua, DartboardCenterX - 3, DartboardCenterY - 3, 6, 6);

			DrawCameras(g);

			pbBoard.Image = b;
		}

	}
}
